package com.bomb.voice.feature.operationAccount;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import com.bigkoo.svprogresshud.SVProgressHUD;
import com.bomb.voice.R;
import com.bomb.voice.bean.AppConfig;
import com.bomb.voice.net.HttpSubscriber;
import com.bomb.voice.utils.AccountConfig;
import com.tbruyelle.rxpermissions.RxPermissions;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import rx.functions.Action1;

public class SplashActivity extends AppCompatActivity {
    private SVProgressHUD svProgressHUD;


    private AlertDialog mAlertView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        RxPermissions.getInstance(this)
                .request(AccountConfig.REQUIRED_PERMISSIONS)
                .subscribe(new Action1<Boolean>() {
                    @Override
                    public void call(Boolean granted) {
                        if (granted) {
                            initView();
                        } else {
                            Toast.makeText(SplashActivity.this, "请同意权限否则无法正常使用APP", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    private void initView() {
        svProgressHUD = new SVProgressHUD(SplashActivity.this);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("连接失败!!点击重试,或者重启应用").setPositiveButton("重试", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                initIntent();

            }
        });
        mAlertView = builder.create();

        initIntent();

    }

    private void initIntent() {
        svProgressHUD.showWithStatus("连接网络电话服务器中...");
//        AppConfig appConfig = new AppConfig();
//        appConfig.setMask(true);
//        appConfig.setRegisterGivingTimes(1500);
//        appConfig.saveObservable().subscribe(new HttpSubscriber<String>(){
//            @Override
//            public void onNext(String s) {
//                super.onNext(s);
//            }
//        });
        BmobQuery<AppConfig> query = new BmobQuery<AppConfig>();

        query.findObjectsObservable(AppConfig.class).subscribe(new HttpSubscriber<List<AppConfig>>() {
            @Override
            public void onNext(List<AppConfig> appConfigs) {
                AppConfig appConfig = appConfigs.get(0);
                svProgressHUD.showSuccessWithStatus("连接成功！输入号码开始拨打吧");
//跳转
            }

            @Override
            public void onError(Throwable throwable) {
                super.onError(throwable);
                //使用默认值 和跳转默认应用
            }
        });

//        if (TextUtils.isEmpty(fyAccountId)) {
//            createFyAccount();
//        } else {
//            loginBmob();
//        }
    }
}
